/**
 * Created by BINGO on 2016/10/31.
 */


//角色列表
var handleRolelist = function () {
    "use strict";
    if ($('#data-table').length !== 0) {
        $('#data-table').DataTable({
            language: {
                url: '/assets/plugins/DataTables/Chinese.json'
            },
            responsive: true,
            lengthMenu: [
                [5, 10, 15, 20, -1],
                [5, 10, 15, 20, "所有"] // change per page values here
            ],
            // set the initial value
            pageLength: 10,
            ajax: {
                "url": "/sysRole/datatable"
            },
            columns: [{
                data: "name",
                title: "角色名称"
            }, {
                data: "type",
                "title": "编码"
            }, {
                data: "description",
                title: "描述"
            }, {
                data: "seq",
                title: "排序"
            }, {
                data: null,
                title: "权限设置"
            }],
            columnDefs: [{
                //   指定第最后一列
                targets: 4,
                render: function (data, type, row, meta) {
                    return '<a class="label label-danger" onclick="role_authority(' + row.id + ');" >授权</a>';
                }
            }],
            ordering: true,
            order: [3, "asc"],
            dom: "<'row' <'col-md-12'B>><'row'<'col-md-6 col-sm-12'l><'col-md-6 col-sm-12'f>r><'table-scrollable't><'row'<'col-md-5 col-sm-12'i><'col-md-7 col-sm-12'p>>"
        });
    }
};

var zTreeObj;
// zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
var setting = {
    view: {
        //showLine: false,
        //showIcon: false
    },
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "pid",
            rootPId: null
        },
        key: {
            name: "name"
        }
    },
    check: {
        chkboxType: {"Y": "ps", "N": "ps"},
        chkStyle: "checkbox",
        enable: true
    }
};

//打开授权窗口
var role_authority = function (rid) {
    layer.open({
        title: "角色授权",
        area: ['360px', '500px'], //宽高
        btn: ['确定'],
        yes: function (index, layero) {
            $.ajax({
                url: "/resource/saveAuth",
                dataType: "json",
                method:"post",
                data: {roleId: rid,resIds:getChecked()},
                success: function (result) {
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                }
            })
        },
        content: '<a class="label label-success m-5" onclick="CheckAllNodes();" >全选</a><a class="label label-danger" onclick="CancelAllNodes();" >全不选</a><ul id="res_tree" class="ztree"></ul>' //注意，如果str是object，那么需要字符拼接。
    });

    $.ajax({
        url: "/resource/layerTree",
        dataType: "json",
        data: {id: rid},
        btn: ['按钮一', '按钮二', '按钮三'],
        success: function (result) {
            var zNodes = result;
            zTreeObj = $.fn.zTree.init($("#res_tree"), setting, zNodes);
            zTreeObj.expandAll(true);
        }
    })
}

//zTree全选
function CheckAllNodes() {
    var treeObj = $.fn.zTree.getZTreeObj("res_tree");
    treeObj.checkAllNodes(true);
}

//zTree全不选
function CancelAllNodes() {
    var treeObj = $.fn.zTree.getZTreeObj("res_tree");
    treeObj.checkAllNodes(false);
}

//zTree获取已选中的节点
function getChecked() {
    var treeObj = $.fn.zTree.getZTreeObj("res_tree"),
        nodes = treeObj.getCheckedNodes(true),
        v = [];
    for (var i = 0; i < nodes.length; i++) {
        v.push(nodes[i].id) //获取选中节点的值
        //v+=nodes[i].name + ",";
    }
    return v;
}

var SysRole = function () {
    "use strict";
    return {
        //main function
        init: function () {
            $.getScript('/assets/plugins/DataTables/js/jquery.dataTables.min.js').done(function () {
                handleRolelist();
            });
        }
    };
}();
